package cn.xzhang.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.xzhang.boot.common.exception.ServiceException;
import cn.xzhang.boot.common.pojo.PageResult;
import cn.xzhang.boot.mapper.WallpaperDownloadRecordMapper;
import cn.xzhang.boot.model.dto.wallpaperDownloadRecord.WallpaperDownloadRecordAddReqDTO;
import cn.xzhang.boot.model.dto.wallpaperDownloadRecord.WallpaperDownloadRecordPageReqDTO;
import cn.xzhang.boot.model.dto.wallpaperDownloadRecord.WallpaperDownloadRecordUpdateReqDTO;
import cn.xzhang.boot.model.entity.WallpaperDownloadRecord;
import cn.xzhang.boot.model.vo.wallpaperDownloadRecord.WallpaperDownloadRecordSimpleVo;
import cn.xzhang.boot.model.vo.wallpaperDownloadRecord.WallpaperDownloadRecordVo;
import cn.xzhang.boot.service.WallpaperDownloadRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.xzhang.boot.common.exception.enums.GlobalErrorCodeConstants.*;
import static cn.xzhang.boot.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 针对表【wallpaperDownloadRecord(壁纸下载记录表)】的数据库操作Service实现
 *
 * @author <a href="https://github.com/XiaoZhangCode">XiaoZhangCode</a>
 */
@Service
public class WallpaperDownloadRecordServiceImpl extends ServiceImpl<WallpaperDownloadRecordMapper, WallpaperDownloadRecord> implements WallpaperDownloadRecordService {

    @Resource
    private WallpaperDownloadRecordMapper wallpaperDownloadRecordMapper;

    /**
     * 添加新壁纸下载记录
     *
     * @param wallpaperDownloadRecordReqDTO 壁纸下载记录信息请求DTO
     * @return 添加成功返回壁纸下载记录id
     */
    @Override
    public long addWallpaperDownloadRecord(WallpaperDownloadRecordAddReqDTO wallpaperDownloadRecordReqDTO) {
        WallpaperDownloadRecord wallpaperDownloadRecord = new WallpaperDownloadRecord();
        BeanUtil.copyProperties(wallpaperDownloadRecordReqDTO, wallpaperDownloadRecord);
        if (!this.save(wallpaperDownloadRecord)) {
            throw exception(ADD_FAIL);
        }
        return wallpaperDownloadRecord.getId();
    }

    /**
     * 更新壁纸下载记录信息
     *
     * @param wallpaperDownloadRecordReqDTO 壁纸下载记录信息更新请求DTO
     * @return 更新成功返回true
     */
    @Override
    public boolean updateWallpaperDownloadRecord(WallpaperDownloadRecordUpdateReqDTO wallpaperDownloadRecordReqDTO) {
        if (wallpaperDownloadRecordReqDTO.getId() == null) {
            throw exception(BAD_REQUEST);
        }
        WallpaperDownloadRecord wallpaperDownloadRecord = new WallpaperDownloadRecord();
        BeanUtil.copyProperties(wallpaperDownloadRecordReqDTO, wallpaperDownloadRecord);
        boolean b = this.updateById(wallpaperDownloadRecord);
        if (!b) {
            throw exception(UPDATE_FAIL);
        }
        return true;
    }

    /**
     * 删除壁纸下载记录
     *
     * @param id 壁纸下载记录id
     * @return 删除成功返回true
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public boolean deleteWallpaperDownloadRecord(Long id) {
        if (id == null) {
            throw exception(BAD_REQUEST);
        }
        boolean b = this.removeById(id);
        if (!b) {
            throw exception(DELETE_FAIL);
        }
        return true;
    }

    /**
     * 将壁纸下载记录对象转换为壁纸下载记录VO对象
     *
     * @param wallpaperDownloadRecord 壁纸下载记录对象
     * @return 返回壁纸下载记录VO对象
     */
    @Override
    public WallpaperDownloadRecordSimpleVo getSimpleWallpaperDownloadRecordVO(WallpaperDownloadRecord wallpaperDownloadRecord) {
        if (wallpaperDownloadRecord == null) {
            return null;
        }
        WallpaperDownloadRecordSimpleVo wallpaperDownloadRecordSimpleVo = new WallpaperDownloadRecordSimpleVo();
        BeanUtil.copyProperties(wallpaperDownloadRecord, wallpaperDownloadRecordSimpleVo);
        return wallpaperDownloadRecordSimpleVo;
    }

    /**
     * 获取壁纸下载记录分页信息
     *
     * @param wallpaperDownloadRecordPageReqDTO 壁纸下载记录分页请求DTO
     * @return 返回壁纸下载记录分页结果
     */
    @Override
    public PageResult<WallpaperDownloadRecordVo> getWallpaperDownloadRecordPage(WallpaperDownloadRecordPageReqDTO wallpaperDownloadRecordPageReqDTO) {
        PageResult<WallpaperDownloadRecord> pageResult = wallpaperDownloadRecordMapper.selectPage(wallpaperDownloadRecordPageReqDTO);
        if (pageResult.getList() == null) {
            return PageResult.empty();
        }
        List<WallpaperDownloadRecordVo> wallpaperDownloadRecordVos = pageResult.getList().stream().map(wallpaperDownloadRecord -> {
            WallpaperDownloadRecordVo wallpaperDownloadRecordVo = new WallpaperDownloadRecordVo();
            BeanUtil.copyProperties(wallpaperDownloadRecord, wallpaperDownloadRecordVo);
            return wallpaperDownloadRecordVo;
        }).collect(Collectors.toList());
        return new PageResult<>(wallpaperDownloadRecordVos, pageResult.getTotal());
    }

    @Override
    public WallpaperDownloadRecordVo getWallpaperDownloadRecordVO(WallpaperDownloadRecord wallpaperDownloadRecord) {
        if (wallpaperDownloadRecord == null) {
            return null;
        }
        WallpaperDownloadRecordVo wallpaperDownloadRecordVo = new WallpaperDownloadRecordVo();
        BeanUtil.copyProperties(wallpaperDownloadRecord, wallpaperDownloadRecordVo);
        return wallpaperDownloadRecordVo;
    }

}



